//
//  VC11Notification.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/30.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC11Notification: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(notifi(no:)), name: (NSNotification.Name(rawValue: "hello")), object: nil)
        
        let rightItem = UIBarButtonItem(title: "取消", style: .done, target: self, action: #selector(clickCancel))
        navigationItem.rightBarButtonItem = rightItem
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let noti = Notification(name: Notification.Name("hello"), object: nil, userInfo: ["key1":"world"])
        NotificationCenter.default.post(noti)
    }
    
    @objc func notifi(no:Notification) -> Void {
        print(no.userInfo ?? "NIL")
    }
    
    @objc func clickCancel() -> Void {
        // func1
        // NotificationCenter.default.removeObserver(self)
        
        // func2
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "hello"), object: nil)
    }
}
